<template>
	<view class="content">
		<view class="bindPhoneTitle">绑定手机号</view>
		<view class="bindPhoneContent">需要绑定手机号才能进行登陆</view>
		<view class="loginWrap">
			<view class="login">
				<input class="inputs" v-model="phone" placeholder="输入手机号" maxlength="11"
					placeholder-class="inputPlaceholder" />
			</view>
			<view class="login">
				<input class="inputs" v-model="code" placeholder="输入验证码" placeholder-class="inputPlaceholder" />
				<text v-if="!isCode" @tap="getCode" class="eyeCode">获取验证码</text>
				<view v-else class="eyeCodes">
					<image @tap="code=''" class="icon_close" src="../../static/img/icon_close.png"></image>
					<text style="color: #BFBFBF;font-size: 28rpx;">{{secondsLeft}}秒后重发</text>
				</view>
			</view>
		</view>
		<view class="submitWrap">
			<view class="submit" @tap="doCompletePhone(true,false)">
				<text style="font-weight: 700;font-size: 32rpx;color: #F5F5F5;letter-spacing: 2rpx;">确定</text>
			</view>
		</view>
	</view>
	<view v-if="isExist" class="popUp">
		<view class="pop" style="height: 350rpx;">
			<view class="popTitle" style="height: 100rpx;">
				<text style="font-weight: 700;font-size: 34rpx;color: #262626;">邀请码（非必填）</text>
			</view>
			<view style="width: 84%;margin-left: 8%;">
				<input class="inputPop" v-model="shareCode" placeholder="请填写邀请码" placeholder-class="inputPlaceholder" />
			</view>
			<view class="existWrap">
				<view @tap="confirmInviteCode(false)" class="left" hover-class="buttonHover">
					<text style="color: #8C8C8C;font-size: 36rpx;">取消</text>
				</view>
				<view @tap="confirmInviteCode(true)" class="right" hover-class="buttonHover">
					<text style="color: #6B64D3;font-size: 36rpx;">确认</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue';
	import { sendSmsCode, completePhone,userPhoneExist,fillShareCode } from '@/utils/api/index.ts'
	import { onShow, onLoad, } from '@dcloudio/uni-app'
	import {connectIm} from '@/utils/tencent.ts'
	import {initPush} from '@/utils/push.ts'
	const code = ref('')
	const phone = ref('')
	const secondsLeft = ref(60)
	const isCode = ref(false)  //手机验证码获取状态
	const timer = ref(null)
	const smsId = ref('')
	const openid=ref('')
	const unionId=ref('')
	const isExist=ref(false)
	const shareCode=ref('')
	const getCode = async () => {
		if (phone.value=='') {
			uni.showToast({
				title: '请填写手机号',
				icon: 'error'
			});
			return
		}
		uni.showLoading({
			title:'请稍后...'
		})
		const res = await sendSmsCode({ data: { phone: phone.value } })
		uni.showToast({
			title: '短信发送成功',
			icon: 'success'
		});
		uni.hideLoading()
		smsId.value = res.data
		isCode.value = true
		secondsLeft.value = 60
		timer.value = setInterval(() => {
			if (secondsLeft.value > 0) {
				secondsLeft.value--
			} else {
				clearInterval(timer.value!)
				isCode.value = false
				secondsLeft.value = 60
			}
		}, 1000);
	}


	const doCompletePhone = async (openFlag:boolean,validShareCode:boolean) => {
		if (!code.value) {
			uni.showToast({
				title: '请输入验证码',
				icon: 'error'
			});
			return
		}
		const res=await completePhone({

			data: {
				phone: phone.value,
				code: code.value,
				smsId: smsId.value,
				openid:openid.value,
				unionId:unionId.value,
				shareCode:shareCode.value
			}

		})
		uni.setStorageSync('token', res.data.token)
		uni.setStorageSync('userInfo', res.data)
		connectIm()
		initPush()
		uni.showToast({
			title: '绑定成功',
			icon: 'success'
		});
		
		setTimeout(()=>{
			uni.switchTab({
				url: `/pages/my/my`
			})
		},500)

	}
 
	const confirmInviteCode=(bool)=>{
		if(bool){
			fillShareCode({data:{shareCode:shareCode.value}}).then(res=>{
				setTimeout(()=>{
					uni.showToast({
						title: '填写成功!',
						icon: 'success'
					});
				},500)
				isExist.value=false
				setTimeout(()=>{
					uni.switchTab({
						url: `/pages/my/my`
					})
				},500)
			})
		}else{
			isExist.value=false
			setTimeout(()=>{
				uni.switchTab({
					url: `/pages/my/my`
				})
			},500)
		}
	}
	
	onLoad((option)=>{
		openid.value=option.openid
		unionId.value=option.unionId
	})
	
	
	
</script>

<style>
	.bindPhoneContent {
		margin-left: 64rpx;
		font-size: 28rpx;
		color: #8C8C8C;
	}

	.bindPhoneTitle {
		margin-top: 80rpx;
		margin-left: 64rpx;
		font-size: 48rpx;
		width: 240rpx;
		height: 68rpx;
	}

	.inputs {
		width: 100%;
		height: 110rpx;
		box-sizing: border-box;
	}

	.loginWrap {
		width: 80%;
		height: 220rpx;
		margin-top:40px;
		margin-left: 10%;
	}

	.login {
		width: 100%;
		height: 110rpx;
		border-bottom: 1rpx solid #EFEEF1;
		display: flex;
		position: relative;
	}

	.eyeCodes {
		width: 220rpx;
		height: 80rpx;
		position: absolute;
		right: 0;
		top: 40rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.eyeCode {
		height: 36rpx;
		color: #6B64D3;
		position: absolute;
		right: 0;
		top: 40rpx;
	}

	.icon_close {
		width: 32rpx;
		height: 32rpx;
		margin-top: 2rpx;
	}

	.inputPlaceholder {
		color: #CED4DB;
		font-size: 32rpx;
	}

	.submitWrap {
		width: 100%;
		height: 160rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.submit {
		width: 638rpx;
		height: 92rpx;
		background: #6B64D3;
		box-shadow: 0rpx 5rpx 24rpx 0rpx rgba(107, 100, 211, 0.3);
		border-radius: 46rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.popUp {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.pop {
		width: 600rpx;
		height: 580rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		position: relative;
	}
	
	.popTitle {
		width: 100%;
		height: 144rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.existWrap {
		width: 100%;
		height: 120rpx;
		position: absolute;
		bottom: 0;
		display: flex;
		flex-direction: row;
	}
	.inputPlaceholder {
		color: #CED4DB;
		font-size: 32rpx;
	}
	.inputPop {
		width: 100%;
		height: 80rpx;
		background: #f4f6fa;
		border-radius: 24rpx;
		padding-left: 20rpx;
	}
	.left {
		width: 50%;
		height: 120rpx;
		border-top: 1rpx solid #ededed;
		border-right: 1rpx solid #ededed;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.right {
		width: 50%;
		height: 120rpx;
		border-top: 1rpx solid #ededed;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.content {
		width: 100%;
		padding-top: var(--status-bar-height);
		min-height: 100vh;
		background: url('../../static/img/bj.png') no-repeat;
	}
</style>